package com.oa.ylms.apps.business.file.impl;

import java.util.List;

import com.oa.ylms.apps.business.file.IFileCategoryService;
import com.oa.ylms.apps.business.file.IFileInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.file.IFileInfoDao;
import com.oa.ylms.apps.entity.file.FileInfoValue;

public class FileInfoServiceImpl implements IFileInfoService {

	private IFileInfoDao fileInfoDao;
	private FileInfoValue fileInfo;
	private IFileCategoryService categoryService;

	
	// Service method
	@Override
	public int addFileInfo(String title, String fileUrl, int categoryId) throws Exception {
		fileInfo.setTitle(title);
		fileInfo.setFileUrl(fileUrl);
		fileInfo.setFileCategory(categoryService.queryCategoryById(categoryId));
		fileInfo.setStates(IDEDStates.ENABLE);
		fileInfo.setCreateTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		return fileInfoDao.addFileInfo(fileInfo);
	}

	@Override
	public boolean deleteFileInfo(String fileIds) throws Exception {
		String[] fileIdStr = fileIds.split(",");
		for(String fileId : fileIdStr){
			fileInfo = queryFileById(Integer.parseInt(fileId));
			if(fileInfo != null){
				fileInfo.setStates(IDEDStates.DELETE);
				fileInfoDao.editFileInfo(fileInfo);
			}else{
				return false;
			}
		}
		return true;
	}
	
	@Override
	public List<FileInfoValue> queryFileInfoWithPage(Integer categoryId, String title, String createTime, Integer states, int rows, int pageNo)throws Exception {
		if(categoryId != null)	
			fileInfo.setFileCategory(categoryService.queryCategoryById(categoryId));
		if(title != null){
			title = "%"+title+"%";
			fileInfo.setTitle(title);
		}
		if(createTime != null)
			fileInfo.setCreateTime(DateUtil.toDateWithoutHours(createTime));
		if(states != null)
			fileInfo.setStates(states);
			return fileInfoDao.qureyFileInfoWithPage(fileInfo, rows, pageNo);
	}

	@Override
	public int countTPages(Integer categoryId, String title, String createTime, Integer states, int rows) throws Exception {
		if(categoryId != null)	
			fileInfo.setFileCategory(categoryService.queryCategoryById(categoryId));
		if(title != null){
			title = "%"+title+"%";
			fileInfo.setTitle(title);
		}
		if(createTime != null)
			fileInfo.setCreateTime(DateUtil.toDateWithoutHours(createTime));
		if(states != null)
			fileInfo.setStates(states);
		return fileInfoDao.countTPages(fileInfo, rows);
	}

	@Override
	public FileInfoValue queryFileById(int fileId) throws Exception {
		fileInfo.setFileId(fileId);
		return fileInfoDao.qureyFileById(fileInfo);
	}
	
	@Override
	public int getTotalNumber () throws Exception{
		return fileInfoDao.getTotalNumbers();
	}
	
	@Override
	public boolean toDisable(int fileId) throws Exception {
		fileInfo = queryFileById(fileId);
		if(fileInfo != null){
			fileInfo.setStates(IDEDStates.DISABLE);
			return fileInfoDao.editFileInfo(fileInfo);
		}
		return false;
	}

	@Override
	public boolean toEnable(int fileId) throws Exception {
		fileInfo = queryFileById(fileId);
		if(fileInfo != null){
			fileInfo.setStates(IDEDStates.ENABLE);
			return fileInfoDao.editFileInfo(fileInfo);
		}
		return false;
	}
	
	@Override
	public List<FileInfoValue> qureyFileInfo(Integer states) throws Exception {
		if(states != null){
			fileInfo.setStates(states);
		}
		return fileInfoDao.qureyFileInfo(fileInfo);
	}

	// Service property accessors

	public IFileInfoDao getFileInfoDao() {
		return fileInfoDao;
	}

	public void setFileInfoDao(IFileInfoDao fileInfoDao) {
		this.fileInfoDao = fileInfoDao;
	}

	public FileInfoValue getFileInfo() {
		return fileInfo;
	}

	public void setFileInfo(FileInfoValue fileInfo) {
		this.fileInfo = fileInfo;
	}

	public IFileCategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(IFileCategoryService categoryService) {
		this.categoryService = categoryService;
	}
}
